package com.pureut.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.equipment.domain.MaintenanceProject;
import com.pureut.equipment.domain.dto.MaintenancePlanDto;
import com.pureut.equipment.domain.dto.MaintenanceProjectDto;
import com.pureut.equipment.domain.vo.MaintenanceProjectVo;
import com.pureut.equipment.mapper.MaintenancePlanMapper;
import com.pureut.equipment.mapper.MaintenanceProjectMapper;
import com.pureut.equipment.service.MaintenanceProjectService;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 *
 * Author:保养项目 实现层
 * Date:2023/04/23 13:05
 * @DESC
 */
@Service
public class MaintenanceProjectServiceImpl extends ServiceImpl<MaintenanceProjectMapper, MaintenanceProject> implements MaintenanceProjectService {

    /**
     * 保养项目mapper
     */
    @Resource
    MaintenanceProjectMapper maintenanceProjectMapper;

    /**
     * 保养计划mapper
     */
    @Resource
    MaintenancePlanMapper maintenancePlanMapper;
    /**
     * 基础模块fegin
     */
    @Resource
    FeignService feignService;


    /**
     * 保养项目列表
     *
     * @param maintenanceProjectVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "maintenance_project")
    public List<MaintenanceProjectDto> getList(MaintenanceProjectVo maintenanceProjectVo) {
        List<MaintenanceProjectDto> list = maintenanceProjectMapper.getList(maintenanceProjectVo);
        //获取保养级别字典
        List<SysDictData> levelArray = DictUtils.getDictCache("maintenance_project_level");
        Map<String, String> levelMap = levelArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        for (MaintenanceProjectDto entity : list) {
            entity.setMaintenanceLevelDict(levelMap.get(entity.getMaintenanceLevel()));
        }
        return list;
    }

    /**
     * 新增
     *
     * @param maintenanceProjectVo
     * @return
     */
    @Override
    public boolean add(MaintenanceProjectVo maintenanceProjectVo) throws Exception {

        MaintenanceProject maintenanceProject = new MaintenanceProject();
        String authorityCoding = feignService.getAuthorityCoding("device:maintenance:project:list");
        if (authorityCoding == null) {
            throw new GlobalException("该单据未进行配置，无法生成单据编码");
        }
        maintenanceProject.setMaintenanceProjectCode(authorityCoding)
                .setMaintenanceProjectName(maintenanceProjectVo.getMaintenanceProjectName())
                .setMaintenancePosition(maintenanceProjectVo.getMaintenancePosition())
                .setMaintenanceLevel(maintenanceProjectVo.getMaintenanceLevel())
                .setWorkingHour(maintenanceProjectVo.getWorkingHour())
                .setMaintenanceContent(maintenanceProjectVo.getMaintenanceContent())
                .setRemark(maintenanceProjectVo.getRemark())
                .setCreateTime(new Date())
                .setCreateBy(SecurityUtils.getUsername())
                .setDeptId(SecurityUtils.getDeptId());

        return save(maintenanceProject);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteMaintenanceProjectById(Long id) {
        //已关联保养计划的项目不允许删除，并且系统提醒”该项目已关联保养计划，无法删除”
        MaintenanceProject maintenanceProject = getById(id);
        long maintenanceProjectId = maintenanceProject.getId();
        List<MaintenancePlanDto> whether = maintenancePlanMapper.isWhether(maintenanceProjectId);
        if(whether.size() > 0){
            throw new GlobalException("该项目已关联保养计划，无法删除");
        }
        return removeById(id);
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public MaintenanceProjectDto getView(Long id) {
        //获取保养级别字典
        List<SysDictData> levelArray = DictUtils.getDictCache("maintenance_project_level");
        Map<String, String> levelMap = levelArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        MaintenanceProjectDto maintenanceProjectView = maintenanceProjectMapper.getMaintenanceProjectView(id);
        maintenanceProjectView.setMaintenanceLevelDict(levelMap.get(maintenanceProjectView.getMaintenanceLevel()));
        return maintenanceProjectView;
    }

    /**
     * 修改
     *
     * @param maintenanceProjectVo
     * @return
     */
    @Override
    public boolean updateMaintenanceProject(MaintenanceProjectVo maintenanceProjectVo) {
        MaintenanceProject byId = getById(maintenanceProjectVo.getId());

        byId.setMaintenanceProjectName(maintenanceProjectVo.getMaintenanceProjectName())
                .setMaintenancePosition(maintenanceProjectVo.getMaintenancePosition())
                .setMaintenanceLevel(maintenanceProjectVo.getMaintenanceLevel())
                .setWorkingHour(maintenanceProjectVo.getWorkingHour())
                .setMaintenanceContent(maintenanceProjectVo.getMaintenanceContent())
                .setRemark(maintenanceProjectVo.getRemark());
        return updateById(byId);
    }
}
